// pages/testdesc/index.js
const app = getApp()
const {
  checkLogin
} = require('../../../utils/util')
const {
  reviewPay,
  userReviewList
} = require("../../../api/api");
const {
  decodeUserInfo
} = require('../../../api/login')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '手机号授权',
    isshow: false,
    info: {},
    bottomLift: app.globalData.bottomLift,
    loginVisible: true,
    bgImg: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(reviewPay)
    if (options.param) {
      const info = JSON.parse(decodeURIComponent(options.param))
      this.setData({
        info
      })
      wx.setNavigationBarTitle({
        title: info.shortTitle + info.title,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 开始测评
  goto(e) {
    checkLogin().then((res) => {
      this.test()
    }).catch((e) => {
      // this.setData({
      //   loginVisible: false,
      // })
      wx.navigateTo({url:'/pages/login/index'})
    })
  },
  async test() {
    let userInfo = wx.getStorageSync('userInfo');
    // 判断用户是否登录
    if (userInfo) {
      // 获取用户测评状态
      const {
        result
      } = await decodeUserInfo()
      if(result.vips.length>0){
        wx.navigateTo({
          url: '/pages/test/question/index?id=' + this.data.info.id,
        })
        return
      }
      const isFreeReview = result.isFreeReview
      wx.setStorageSync('userInfo', result);
      var that = this
      const mobile = wx.getStorageSync('mobile')
      if (!mobile) {
        that.setData({
          isshow: true
        })
        return
      }
      const params = {
        reviewId: this.data.info.id,
        status: 1,
      }
      userReviewList(params).then((res) => {
        if (res.result.length > 0 && isFreeReview === 1 && res.code !== 404) {
          wx.navigateTo({
            url: '/pages/test/question/index?id=' + that.data.info.id,
          })
        } else {
          // 获取测评支付
          reviewPay().then((res) => {
            wx.requestPayment({
              nonceStr: res.result.nonceStr,
              package: res.result.package,
              paySign: res.result.paySign,
              timeStamp: res.result.timeStamp,
              signType: res.result.signType,
              success: async function (res) {
                wx.showToast({
                  icon: 'success',
                  title: '支付完成',
                })
                wx.navigateTo({
                  url: '/pages/test/question/index?id=' + that.data.info.id,
                })
              },
              fail: function (res) {

              }
            })
          })
        }
      })



    }
  },
  my(e) {
    wx.navigateTo({
      url: '/pages/users/test/index'
    })
  },
  onShareAppMessage: function () {
    return {
      title: '职业测评',
      imageUrl: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  },
})
